/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include ".\mconfig.h"

//---------------------------------------------------------------------------

MConfig::MConfig(void) : MFileConfig()
{
	Initialize();
}

MConfig::~MConfig(void)
{
}

//---------------------------------------------------------------------------

void MConfig::Initialize(void)
{
	theMap.Empty();
	theSkin                  = _T("BaseSkin");

	mapWndRect.left          = 17;
	mapWndRect.top           = 44;
	mapWndRect.right         = 361;
	mapWndRect.bottom        = 499;

	msgWndRect.left          = 145;
	msgWndRect.top           = 487;
	msgWndRect.right         = 617;
	msgWndRect.bottom        = 642;

	detailsWndRect.left      = 364;
	detailsWndRect.top       = 44;
	detailsWndRect.right     = 761;
	detailsWndRect.bottom    = 371;

	scanSelCfgWndRect.left   = 0;
	scanSelCfgWndRect.top    = 0;
	scanSelCfgWndRect.right  = 504;
	scanSelCfgWndRect.bottom = 280;

	notifTcp                 = 1000;
	notifUdp                 = 30;
	notifPing                = 100;
	notifNetBios             = 25;

	idLen                    = 22;
}

//---------------------------------------------------------------------------

void MConfig::LoadFromPointer(MConfig* cfgIn)
{
	theMap                   = cfgIn->theMap;
	theSkin                  = cfgIn->theSkin;

	mapWndRect.left          = cfgIn->mapWndRect.left;
	mapWndRect.top           = cfgIn->mapWndRect.top;
	mapWndRect.right         = cfgIn->mapWndRect.right;
	mapWndRect.bottom        = cfgIn->mapWndRect.bottom;

	msgWndRect.left          = cfgIn->msgWndRect.left;
	msgWndRect.top           = cfgIn->msgWndRect.top;
	msgWndRect.right         = cfgIn->msgWndRect.right;
	msgWndRect.bottom        = cfgIn->msgWndRect.bottom;

	detailsWndRect.left      = cfgIn->detailsWndRect.left;
	detailsWndRect.top       = cfgIn->detailsWndRect.top;
	detailsWndRect.right     = cfgIn->detailsWndRect.right;
	detailsWndRect.bottom    = cfgIn->detailsWndRect.bottom;

	scanSelCfgWndRect.left   = cfgIn->scanSelCfgWndRect.left;
	scanSelCfgWndRect.top    = cfgIn->scanSelCfgWndRect.top;
	scanSelCfgWndRect.right  = cfgIn->scanSelCfgWndRect.right;
	scanSelCfgWndRect.bottom = cfgIn->scanSelCfgWndRect.bottom;

	notifTcp                 = cfgIn->notifTcp;
	notifUdp                 = cfgIn->notifUdp;
	notifPing                = cfgIn->notifPing;
	notifNetBios             = cfgIn->notifNetBios;

	idLen                    = cfgIn->idLen;
}

//---------------------------------------------------------------------------

CString MConfig::BuildId(CString strIn)
{
	CString str;

	str = strIn;
	while(str.GetLength()<22)
		str += _T(" ");
	str += _T(" = ");

	return str;
}

//---------------------------------------------------------------------------

void MConfig::InternalInit(void)
{
	Initialize();
}

//---------------------------------------------------------------------------

bool MConfig::InternalProcessLine(char* buf)
{
	CString str, strId;
	int pos;
	int val;

	if(buf==NULL){
		// nothing to process
		return true;
	}

	str = buf;
	pos = str.Find('=');
	if(pos==-1){
		// invalid line
		return false;
	}

	strId = str.Left(pos);
	strId = strId.Trim();

	str = str.Mid(pos+1);
	str = str.Trim();

	if(strId.CompareNoCase("Skin")==0){
		theSkin = str;
		return true;
	}
	if(strId.CompareNoCase("Map")==0){
		theMap = str;
		return true;
	}
	if(strId.CompareNoCase("CompletionBeep")==0){
		completionBeep = BoolFromString(str);
		return true;
	}
	if(strId.CompareNoCase("MapWndRect")==0){
		return RectFromString(&mapWndRect, str);
	}
	if(strId.CompareNoCase("MsgWndRect")==0){
		return RectFromString(&msgWndRect, str);
	}
	if(strId.CompareNoCase("DetailsWndRect")==0){
		return RectFromString(&detailsWndRect, str);
	}
	if(strId.CompareNoCase("ScanSelWndRect")==0){
		return RectFromString(&scanSelCfgWndRect, str);
	}

	if(strId.CompareNoCase("NotifyTCP")==0){
		val = IntFromString(str);
		if(val<NOTIF_TCP_MIN) val = NOTIF_TCP_MIN;
		if(val>NOTIF_TCP_MAX) val = NOTIF_TCP_MAX;
		notifTcp = val;
		return true;
	}
	if(strId.CompareNoCase("NotifyUDP")==0){
		val = IntFromString(str);
		if(val<NOTIF_UDP_MIN) val = NOTIF_UDP_MIN;
		if(val>NOTIF_UDP_MAX) val = NOTIF_UDP_MAX;
		notifUdp = val;
		return true;
	}
	if(strId.CompareNoCase("NotifyPing")==0){
		val = IntFromString(str);
		if(val<NOTIF_ICMP_MIN) val = NOTIF_ICMP_MIN;
		if(val>NOTIF_ICMP_MAX) val = NOTIF_ICMP_MAX;
		notifPing = val;
		return true;
	}
	if(strId.CompareNoCase("NotifyNetBios")==0){
		val = IntFromString(str);
		if(val<NOTIF_L4_MIN) val = NOTIF_L4_MIN;
		if(val>NOTIF_L4_MAX) val = NOTIF_L4_MAX;
		notifNetBios = val;
		return true;
	}

	// unknown directive

	return true;
}

//---------------------------------------------------------------------------

bool MConfig::InternalSave(void)
{
	if(!WriteEmptyCommentLine()) return false;
	if(!WriteCommentLine(_T("CHScanner - Configuration File"))) return false;
	// AiciNU AiciNU AiciNU AddTimeStampInComment !!!
	if(!WriteEmptyCommentLine()) return false;

	if(!WriteEmptyLine()) return false;

	if(!WriteLineWithId(_T("Skin"),           theSkin)) return false;
	if(!WriteLineWithId(_T("Map"),            theMap)) return false;
	if(!WriteLineWithId(_T("CompletionBeep"), BoolToString(completionBeep))) return false;

	if(!WriteEmptyLine()) return false;

	if(!WriteLineWithId(_T("MapWndRect"),     RectToString(&mapWndRect))) return false;
	if(!WriteLineWithId(_T("MsgWndRect"),     RectToString(&msgWndRect))) return false;
	if(!WriteLineWithId(_T("DetailsWndRect"), RectToString(&detailsWndRect))) return false;
	if(!WriteLineWithId(_T("ScanSelWndRect"), RectToString(&scanSelCfgWndRect))) return false;

	if(!WriteEmptyLine()) return false;

	if(!WriteLineWithId(_T("NotifyTCP"),      IntToString(notifTcp))) return false;
	if(!WriteLineWithId(_T("NotifyUDP"),      IntToString(notifUdp))) return false;
	if(!WriteLineWithId(_T("NotifyPing"),     IntToString(notifPing))) return false;
	if(!WriteLineWithId(_T("NotifyNetBios"),  IntToString(notifNetBios))) return false;

	if(!WriteEmptyLine()) return false;

	return true;
}

//---------------------------------------------------------------------------
